﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;
namespace TestDxfDocument
{
    class class_ISYC_201_07
    {
        public static void ISYC_201_07(Dictionary<string, Object> data) //凸三角形刀片
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour");//轮廓层
            Layer dimensioning = new Layer("dimensioning");//尺寸层
            Layer refin = new Layer("refin");//基准线/内轮廓/虚线
            Layer center = new Layer("center");//中心线
            Layer textlayer = new Layer("textlayer");//文本层
            Layer hatching = new Layer("hatching");//填充层
            Layer additionalline = new Layer("additionalline");//附加线
            Layer cutting = new Layer("cutting");//切削层
            fun.SetLayer(dxf);

            //基本参数-------------------------------------------------------------------------------------------------
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            //赋值参数--------------------------------------------------------------------------------------------------
            double IC = Convert.ToDouble(data["ic"]);
            double EPSR = Convert.ToDouble(data["epsr"]);
            double S = Convert.ToDouble(data["s"]);
            double RE = Convert.ToDouble(data["re"]);
            double REN = Convert.ToDouble(data["ren"]);
            double L = Convert.ToDouble(data["l"]);
            double AN = Convert.ToDouble(data["an"]);
            double scale = IC / 12.7;//放大系数

            //点---------------------------------------------------------------------------------------------
            //第一部分
            //轮廓
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v1_1 = fun.Polar(v1, EPSR * pi / 180, L / 2.0);
            Vector2 v2 = fun.Polar(v1, EPSR * pi / 180, L);
            Vector2 v6 = fun.Polar(v1, 0 * pi / 180, L);
            Vector2 v7 = fun.Inters(v1, fun.Polar(v1, EPSR / 2.0 * pi / 180, L * 2), v6, fun.Polar(v6, (EPSR / 2.0 + 60) * pi / 180, L * 2));
            Vector2 v3 = fun.rotatePoint_Changed(v7, v1, -120);
            Vector2 v4 = fun.rotatePoint_Changed(v7, v2, -120);
            Vector2 v5 = fun.rotatePoint_Changed(v7, v3, -120);
            Vector2 v8 = fun.Polar(v6, 0 * pi / 180, L * 4);
            Vector2 v9 = fun.Polar(v8, (180 - AN) * pi / 180, S / Math.Cos(AN * pi / 180));
            Vector2 v11 = new Vector2(v8.X, v8.Y + Math.Abs(v3.Y - v1.Y));
            Vector2 v10 = fun.Inters(v9, fun.Polar(v9, 90 * pi / 180, L), v11, fun.Polar(v11, -(180 - AN) * pi / 180, L));
            Vector2 v12 = new Vector2(v8.X, v7.Y);

            //作图部分-----------------------------------------------------------------------
            //第一部分
            //轮廓
            LwPolyline polyline1 = new LwPolyline();
            polyline1.Vertexes.Add(new LwPolylineVertex(v1_1));
            polyline1.Vertexes.Add(new LwPolylineVertex(v2));
            polyline1.Vertexes.Add(new LwPolylineVertex(v3));
            polyline1.Vertexes.Add(new LwPolylineVertex(v4));
            polyline1.Vertexes.Add(new LwPolylineVertex(v5));
            polyline1.Vertexes.Add(new LwPolylineVertex(v6));
            polyline1.Vertexes.Add(new LwPolylineVertex(v1));
            polyline1.Vertexes.Add(new LwPolylineVertex(v1_1));
            fun.PolyFill(dxf, polyline1, 0.05, contour);

            //倒角
            var arc1 = Pus.Fillet4LwPline(polyline1, 1, REN);
            var arc2 = Pus.Fillet4LwPline(arc1, 3, RE);
            var arc3 = Pus.Fillet4LwPline(arc2, 5, REN);
            var arc4 = Pus.Fillet4LwPline(arc3, 7, RE);
            var arc5 = Pus.Fillet4LwPline(arc4, 9, REN);
            var arc6 = Pus.Fillet4LwPline(arc5, 11, RE);
            fun.PolyFill(dxf, arc6, 0.3, contour);

            //虚线
            Circle circle1 = new Circle(v7, IC / 2.0);
            circle1.Layer = refin;
            circle1.LinetypeScale = 5;
            circle1.Linetype = Linetype.Dashed;
            dxf.AddEntity(circle1);

            //标注---------------------------------------------------------------------------
            double height = 1 * scale;
            double arrowsize = 0.5 * scale;
            double exline = 0.5 * scale;

            //IC
            Vector2 bz1 = fun.getPointForArcandLine(v7, fun.Polar(v7, 0 * pi / 180, IC), v7, new Arc(v7, IC / 2.0, 0, 360));
            Vector2 bz2 = fun.getPointForArcandLine(v7, fun.Polar(v7, 180 * pi / 180, IC), v7, new Arc(v7, IC / 2.0, 0, 360));
            fun.dimrot1(dxf, bz1, bz2, 180, 0, 0.1, height, arrowsize, exline, "IC", "", "");

            //L
            fun.dimrot1(dxf, v1, v6, 180, L * 0.5, 0.1, height, arrowsize, exline, "L", "", "");

            //角度标注
            //EPSR
            fun.jdbz2(dxf, new Line(v3, fun.Polar(v3, fun.CalAngle(v2, v3), 0.1)), new Line(v3, fun.Polar(v3, fun.CalAngle(v4, v3), 0.1)), S, height, arrowsize, exline, "EPSR", "", "");

            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }
            fun.SetLayer(dxf);
            fun.SetByLayer(dxf);

            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
